﻿$.extend({
    getUrlVars: function() {

        var vars = [], hash;
        var href = window.location.href.split('#')[0];
        if (href.indexOf('?') < 0) return vars;
        var hashes = href.slice(href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    },
    getUrlVar: function(name) {
        return $.getUrlVars()[name];
    }    
});


function RemoveSign4VietnameseString(str) {  
      //str= str.toLowerCase();
      str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g, "a");
      str = str.replace(/Á|À|Ạ|Ả|Ã|Â|Ấ|Ầ|Ậ|Ẩ|Ẫ|Ă|Ắ|Ằ|Ặ|Ẳ|Ẵ/g, "A");
      str = str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g, "e");
      str = str.replace(/É|È|Ẹ|Ẻ|Ẽ|Ê|Ế|Ề|Ệ|Ể|Ễ/g, "E");
      str = str.replace(/ì|í|ị|ỉ|ĩ/g, "i");
      str = str.replace(/Í|Ì|Ị|Ỉ|Ĩ/g, "i");
      str= str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g,"o");  
      str = str.replace(/Ó|Ò|Ọ|Ỏ|Õ|Ô|Ố|Ồ|Ộ|Ổ|Ỗ|Ơ|Ớ|Ờ|Ợ|Ở|Ỡ/g, "i");
      str= str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g,"u");  
      str = str.replace(/Ú|Ù|Ụ|Ủ|Ũ|Ư|Ứ|Ừ|Ự|Ử|Ữ/g, "i");
      str= str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g,"y");
      str = str.replace(/Ý|Ỳ|Ỵ|Ỷ|Ỹ/g, "Y");
      str = str.replace(/đ/g, "d");
      str = str.replace(/Đ/g, "D");  
      str= str.replace(/!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'|\"|\&|\#|\[|\]|~|$|-/g,"");
      /* tìm và thay thế các kí tự đặc biệt trong chuỗi sang kí tự - */
      str = str.replace(/\s+/g, " ");
      //str= str.replace(/_+_/g,"_"); //thay thế 2- thành 1- 
      //str= str.replace(/^\-+|\-+$/g,"");  
      //cắt bỏ ký tự - ở đầu và cuối chuỗi  
      return str;
  }
  function RemoveSign4Url(str) {
      return RemoveSign4VietnameseString(str).replace(/ /g,"_");
  }

  /**
  *
  *  URL encode / decode
  *  http://www.webtoolkit.info/
  *
  **/

  var Url = {

      // public method for url encoding
      encode: function(string) {
          return escape(this._utf8_encode(string));
      },

      // public method for url decoding
      decode: function(string) {
          return this._utf8_decode(unescape(string));
      },

      // private method for UTF-8 encoding
      _utf8_encode: function(string) {
          string = string.replace(/\r\n/g, "\n");
          var utftext = "";

          for (var n = 0; n < string.length; n++) {

              var c = string.charCodeAt(n);

              if (c < 128) {
                  utftext += String.fromCharCode(c);
              }
              else if ((c > 127) && (c < 2048)) {
                  utftext += String.fromCharCode((c >> 6) | 192);
                  utftext += String.fromCharCode((c & 63) | 128);
              }
              else {
                  utftext += String.fromCharCode((c >> 12) | 224);
                  utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                  utftext += String.fromCharCode((c & 63) | 128);
              }

          }

          return utftext;
      },

      // private method for UTF-8 decoding
      _utf8_decode: function(utftext) {
          var string = "";
          var i = 0;
          var c = c1 = c2 = 0;

          while (i < utftext.length) {

              c = utftext.charCodeAt(i);

              if (c < 128) {
                  string += String.fromCharCode(c);
                  i++;
              }
              else if ((c > 191) && (c < 224)) {
                  c2 = utftext.charCodeAt(i + 1);
                  string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                  i += 2;
              }
              else {
                  c2 = utftext.charCodeAt(i + 1);
                  c3 = utftext.charCodeAt(i + 2);
                  string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                  i += 3;
              }

          }

          return string;
      }

  }
  jQuery.fn.outer = function() {
      return $($('<div></div>').html(this.clone())).html();
  }

function setCookie(key, val, days) {
 var str = key + "=" + val + "; domain=.pulsemgr.com; path=/";
 if (days) {
 var date = new Date();
 date.setTime(date.getTime()+(days*86400*1000));
 str = str + "; expires=" + date.toGMTString();
 }
 document.cookie = str;
}

function getCookie(key) {
     var match = key + "=";
     var cookies = document.cookie.split(';');
     for (var i=0; i<cookies.length; i++) {
         var c = cookies[i];
         while (c.charAt(0)==' ') { c = c.substring(1,c.length); }
         if (c.indexOf(match) == 0) {
         return c.substring(match.length, c.length);
        }
     }
     return null;
}

function deleteCookie(key) {
 setCookie(key, "", -1);
}

function numToString(num) {
    num = num.toString();
    var result = "";    
    for (var i = 0; i < num.length; i++) {
        if (i % 3 == 0 && i > 0) {
            result = "." + result;
        }
        result = num.charAt(num.length - i - 1).toString() + result.toString();        
    }
    return result;
}
